package com.mapre.instance.projoin;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author shengtao
 * @Description:
 * @date 2019/01/14 16:21
 */
public class JoinMR extends Configured implements Tool {

    public static void main(String[] args) throws Exception {
        ToolRunner.run(new JoinMR(),args);

        System.out.println("processed result is not success！");
    }

    @Override
    public int run(String[] args) throws Exception {

        String nameNodeUrl = "hdfs://192.168.220.135:9000";
        String nameNodeName = "fs.defaultFS";
        Configuration configuration = new Configuration();
        configuration.set(nameNodeName, nameNodeUrl);
        Job job = Job.getInstance(configuration);

        job.setJarByClass(JoinMR.class);

        job.setMapperClass(JoinMapper.class);
        job.setReducerClass(JoinMReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(InfoBean.class);

        job.setOutputKeyClass(InfoBean.class);
        job.setOutputValueClass(NullWritable.class);

        FileInputFormat.setInputPaths(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));

        boolean result = job.waitForCompletion(true);
        if (!result){
            return 1;
        }
        return 0;
    }

    static class JoinMapper extends Mapper<LongWritable,Text,Text,InfoBean>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String s[] = line.split("\t");

            FileSplit split = (FileSplit) context.getInputSplit();
            String fileName = split.getPath().getName();
            InfoBean infoBean = new InfoBean();
            Text k = new Text();
            String pid = "";
            if(fileName.startsWith("order")){
                //订单
                pid = s[2];
                infoBean.set(Integer.parseInt(s[0]),s[1],pid,Integer.parseInt(s[3]),"",0,0,"0");
            }else {
                //商品
                pid = s[0];
                infoBean.set(0,"",pid,0,s[1],Integer.parseInt(s[2]),Float.valueOf(s[3]),"1");
            }
            k.set(pid);
            context.write(k,infoBean);
        }
    }

    static class JoinMReducer extends Reducer<Text,InfoBean,InfoBean,NullWritable>{

        @Override
        protected void reduce(Text key, Iterable<InfoBean> values, Context context) throws IOException, InterruptedException {
            InfoBean infoBean = new InfoBean();

            List<InfoBean> infoBeans = new ArrayList<>();
            try {
                for (InfoBean value:values){
                    System.out.println("======================:"+value.toString());
                    if(value.getFlag().equals("1")){
                        //商品
                        BeanUtils.copyProperties(infoBean,value);
                    }else {
                        //订单
                        InfoBean odBean = new InfoBean();
                        BeanUtils.copyProperties(odBean,value);
                        infoBeans.add(odBean);
                    }
                    System.out.println("======================:"+infoBean.toString());
                    for (InfoBean bean:infoBeans){
                        bean.setPname(infoBean.getPname());
                        bean.setCategory_id(infoBean.getCategory_id());
                        bean.setPrice(infoBean.getPrice());
                        context.write(bean, NullWritable.get());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

}
